﻿using System;
using System.Reflection;
using LitJson;

namespace QuickConvert.Json
{
    public class JsonToJsonConfig
    {
        public string OriStructure;
        public string ConvertStructure;

        public JsonData ToJsonData()
        {
            return JsonMapper.ToObject(ConvertStructure);
        }

        public static string ConvertTo(SingleItemConvertStructure structure, string toConvert)
        {
            Assembly asm = Assembly.Load(structure.AssemblyName);

            Type t = Assembly.Load(structure.AssemblyName).GetType(structure.ConvertFuncType);
            object[] args = { toConvert };

            string res = String.Empty;

            if (structure.IsStaticClass)
            {
                throw new Exception("Static class is not allowed!");
            }
            else
            {
                Object obj = asm.CreateInstance(structure.ConvertFuncType, true, BindingFlags.Default, null, null, null, null);
                res = (string) t.InvokeMember(structure.ConvertFuncName, BindingFlags.InvokeMethod, null, obj, args);
            }

            return res;
        }
    }
}
